﻿using System;
using EscapeZone.Components;
using EscapeZone.Interfaces;
using EscapeZone.Serialization;
using Microsoft.Xna.Framework;
using XnaPlus.Common.Extensions;
using XnaPlus.Common.Visual;
using System.Collections.Generic;
using XnaPlus.StateManagement.Screens;

namespace EscapeZone.Mapping.Elements
{
    public class Guard : DrawableEscapeGameComponent, IAnimationHook, IPhysical, IDefinable
    {

        #region Constants

        private const float GuardSpeed = 3.0f;

        #endregion

        #region Fields

        private Animation m_animation;

        private int m_targetIndex = 1;

        private Direction m_currentDirection = Direction.Foward;

        #endregion

        public Vector2 Position { get; set; }

        public bool Centered
        {
            get { return true; }
        }

      

        public Rectangle Rect
        {
            get { return new Rectangle((int)Position.X, (int)Position.Y, m_animation.Size.X, m_animation.Size.Y); }
        }

        private List<Vector2> m_checkpoints;

        public List<Vector2> Checkpoints
        {
            get { return m_checkpoints; }
            set
            {
                m_checkpoints = value;
                if (value == null)
                {
                    Position = Vector2.Zero;
                }
                else if (value.Count > 0)
                {
                    Position = value[0];
                }
            }
        }


        public Guard(EscapeGame game, Screen screen, GuardDefinition definition)
            : base(game, screen)
        {
            Position = definition.Position;
            Checkpoints = definition.Checkpoints;

            m_animation = new Animation(game, Art.Guard, new Point(3, 1), game.SpriteBatch, this)
            {
                IsLooping = true,
                UpdateDelay = TimeSpan.FromMilliseconds(200),
                Scale = 2f,
            };
        }



        public IDefinition GetDefinition()
        {
            return new GuardDefinition { Position = Position, Checkpoints = Checkpoints };
        }

        public override void Update(GameTime gameTime)
        {
            Vector2 target = Checkpoints[m_targetIndex];
            
            //displacement
            Vector2 displacement = (target - Position).Unit() * GuardSpeed;

            //Check if arrived at the next checkpoint
            if ((target - Position).Length() <= displacement.Length())
            {
                Position = target;

                m_targetIndex += (int)m_currentDirection;

                if (m_targetIndex <= 0 || m_targetIndex >= Checkpoints.Count - 1)
                {
                    m_currentDirection = (Direction)(-(int)m_currentDirection);
                }
            }
            else
            {
                Position += displacement;
            }

            m_animation.Orientation = (float)(displacement.Theta() + Math.PI / 2);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (m_animation.Visible)
            {
                m_animation.Draw(gameTime);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                m_animation.Dispose();
                m_animation = null;
            }

        }


        public ElementType Type
        {
            get { return ElementType.Guard; }
        }
    }

}

